package LeetCode;

import Util.ListNode;

/**
 * @author attcat
 * ---做一个有趣的程序员
 * @date 2022-10-17 21:57
 * @description
 * 删除重复节点
 * 给出1 1 2 3 3
 */
public class LeetCode_83 {
    public static void main(String[] args) {
        ListNode l1=new ListNode(1,new ListNode(1,new ListNode(3,new ListNode(3,new ListNode(4)))));
        LeetCode_83 leetCode_83=new LeetCode_83();
        ListNode p= leetCode_83.delecteDuplicates2(l1);
        System.out.println(p.toString());
    }

    /***
     * 循环
     * @param head
     * @return
     */
    public ListNode delecteDuplicates(ListNode head){
        if (head==null){
            return  head;
        }
        ListNode currentNode=head;
        while (null!=currentNode.next){
            if(currentNode.val==currentNode.next.val){
                currentNode.next=currentNode.next.next;
            }else {
                currentNode=currentNode.next;
            }
        }
        return  head;
    }
    /***
     * 递归的本质，压栈 后倒序 处理
     */
    public ListNode delecteDuplicates2(ListNode head){
        if(head==null || head.next==null)return  head;
        head.next=delecteDuplicates2(head.next);
        return head.val== head.next.val?head.next:head;
    }

}
